package schoolWork.work05.Example2c;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//采用匿名类实现事件侦听者ActionListener
public class MyFrm_compute3 extends JFrame {
    JLabel lbl_number = new JLabel("请输入一个数:");

    JTextField txt_number = new JTextField(10);

    JButton btn_square = new JButton("它的平方是:");

    JButton btn_sqrt = new JButton("它的平方根是:");

    JTextField txt_square = new JTextField(10);

    JTextField txt_sqrt = new JTextField(10);

    public MyFrm_compute3() {
        setLayout(new GridLayout(3, 3));
        add(lbl_number);
        add(txt_number);
        add(btn_square);
        add(txt_square);
        add(btn_sqrt);
        add(txt_sqrt);

        btn_square.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int number = Integer.parseInt(txt_number.getText().trim());
                txt_square.setText(Math.pow(number, 2) + "");
            }
        });
        btn_sqrt.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int number = Integer.parseInt(txt_number.getText().trim());
                txt_sqrt.setText(Math.sqrt(number) + "");
            }
        });

        setSize(300, 160);
        setVisible(true);
        setTitle("计算");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}